package cn.zhz.customview.view.tiktok;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;
import cn.zhz.customview.R;

/**
 * @project: customViewDemo
 * @author: ZhuangHZ
 * @createTime: 2019/6/9 10:04
 * @describe: 1、首先先绘制圆形图片
 * 2、再根据贝塞尔曲线绘制小图标的行走路径
 * 3、根据行走路径绘制音符
 * @version:
 */
public class TikTokMusicView extends View {

    private Paint paint;
    private Paint innerPaint;
    private Bitmap nBitmap;
    private Bitmap musicBitmap1;
    private Bitmap musicBitmap2;
    private int radius = 150; //圆形图片的半径
    private int margin = 50;//图片距离右边和下边的边距
    private ValueAnimator rotateAnimator;
    private int mWidth, mHeight;
    private float bitmapCX, bitmapCY;//圆形图片的圆心
    private float bitmapLTX, bitmapLTY;//圆形图片的左上角X和Y的坐标位置


    public TikTokMusicView(Context context) {
        this(context, null);
    }

    public TikTokMusicView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TikTokMusicView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        musicPath = new MusicPath();
        musicPath2 = new MusicPath();
        musicPath3 = new MusicPath();
        musicPath4 = new MusicPath();
        musicPath5 = new MusicPath();
        paint = new Paint();
        paint.setFilterBitmap(true);
        innerPaint = new Paint();
        innerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        innerPaint.setFilterBitmap(true);
        innerPaint.setAntiAlias(true);
        nBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.img_head);
        nBitmap = Bitmap.createScaledBitmap(nBitmap, radius * 2, radius * 2, true);

        rotateAnimator = ValueAnimator.ofFloat(0f, 1.0f);
        rotateAnimator.setInterpolator(new LinearInterpolator());
        rotateAnimator.setDuration(4000);
        rotateAnimator.setRepeatCount(-1);
        rotateAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                postInvalidate();
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //这边计算宽高  将圆形图片放在右下角，含边距
        mWidth = radius * 3 + margin;
        mHeight = radius * 3 + margin;
        setMeasuredDimension(mWidth, mHeight);

        bitmapCX = mWidth - margin - radius;
        bitmapCY = mHeight - margin - radius;
        bitmapLTX = mWidth - margin - 2 * radius;
        bitmapLTY = mHeight - margin - 2 * radius;
        initPath();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCircleBitmap(canvas);
        musicPath.onDraw(canvas);
        musicPath2.onDraw(canvas);
        musicPath3.onDraw(canvas);
        musicPath4.onDraw(canvas);
        musicPath5.onDraw(canvas);
    }




    /**
     * 绘制旋转圆形图片
     */
    private void drawCircleBitmap(Canvas canvas) {
        canvas.save();
        int sc = canvas.saveLayer(bitmapLTX, bitmapLTY, bitmapLTX + nBitmap.getWidth(), bitmapLTY + nBitmap.getHeight(), null, Canvas.ALL_SAVE_FLAG);//创建一个新的layer
        canvas.drawCircle(bitmapCX, bitmapCY, radius, paint);
        canvas.rotate((float) rotateAnimator.getAnimatedValue() * 360, bitmapCX, bitmapCY);
        canvas.drawBitmap(nBitmap, bitmapLTX, bitmapLTY, innerPaint);
        canvas.restoreToCount(sc);
        canvas.restore();
    }

    private MusicPath musicPath;
    private MusicPath musicPath2;
    private MusicPath musicPath3;
    private MusicPath musicPath4;
    private MusicPath musicPath5;


    private void initPath() {
        musicBitmap1 = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.music1);
        musicBitmap2 = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.music2);

        Path path = new Path();
        path.moveTo(bitmapCX, bitmapCY + radius);
        path.quadTo(0, bitmapCY + radius, bitmapCX - radius, radius / 2);

        Path path1 = new Path();
        path1.moveTo(bitmapCX, bitmapCY + radius);
        path1.quadTo(0, bitmapCY + radius, bitmapCX - radius * 1.1f, radius / 2);

        Path path2 = new Path();
        path2.moveTo(bitmapCX, bitmapCY + radius);
        path2.cubicTo(0, bitmapCY + radius, bitmapCX - radius * 1.1f, radius , radius/4, radius/2);


        musicPath.init(path, musicBitmap1);
        musicPath2.init(path1, musicBitmap1);
        musicPath3.init(path2, musicBitmap2);
        musicPath4.init(path, musicBitmap1);
        musicPath5.init(path1, musicBitmap2);
    }



    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (rotateAnimator != null) {
            rotateAnimator.start();
        }
        if (musicPath != null) {
            musicPath.start();
        }
        if (musicPath2 != null) {
            musicPath2.start(800);
        }
        if (musicPath3 != null) {
            musicPath3.start(1600);
        }
        if (musicPath4 != null) {
            musicPath4.start(2400);
        }
        if (musicPath5 != null) {
            musicPath5.start(3200);
        }
    }
}
